package com.glut.gultguy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.glut.gultguy.To.goodsTo;
import com.glut.gultguy.entity.Goods;
import com.glut.gultguy.entity.Images;
import com.glut.gultguy.mapper.GoodsMapper;
import com.glut.gultguy.mapper.ImagesMapper;
import com.glut.gultguy.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glut.gultguy.utils.DateUtils;
import com.glut.gultguy.utils.SnowflakeIdWorker;
import com.glut.gultguy.vo.goodsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-05
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired(required = false)
    private GoodsMapper mapper;

    @Autowired(required = false)
    private ImagesMapper imagesMapper;

    @Autowired
    private DateUtils dateUtils;

    @Autowired
    private SnowflakeIdWorker idWorker;
    @Override
    public List<goodsVo> getList() {
        List<Goods> goods = mapper.selectList(new QueryWrapper<Goods>().eq("goodsState",2));
       return this.GoodsVoRes(goods);
    }

    @Override
    public int addClick(String goodsID) {
        int t = this.baseMapper.addClick(goodsID);
        return t;
    }

    @Override
    public Map<String,Integer> upGoods(goodsTo to) {
        Goods goods =  new Goods();
        String id = idWorker.nextId()+"";
        goods.setId(id);
        goods.setClass_id(to.getClass_id());
        goods.setStart_price(to.getStart_price());
        goods.setGoodsState(0);
        goods.setAuctioning(to.getAuctioning());
        goods.setDescribes(to.getDescribe());
        goods.setName(to.getName());
        goods.set_updateTime(new Date(to.getStart_time()));
        goods.setEnd_time(new Date(to.getEnd_time()));
        goods.setStart_time(new Date(to.getStart_time()));
        goods.setClicks(to.getClicks());
        goods.setCurrent_price(to.getCurrent_price());
        goods.setPublisher_id(to.getPublisher_id());
        goods.setAddTime(new Date());
        to.getImages().stream().map(imge->{
            Images images = new Images();
            images.set_id(idWorker.nextId()+"");
            images.setGoods_id(id);
            images.setImagesUrl(imge);
            imagesMapper.insert(images);
            return null;
        }).collect(Collectors.toList());

      int t =  mapper.insert(goods);
        Map<String,Integer> map = new HashMap<>();
        if (t==1){
            map.put("code",200);
        }else {
            map.put("code",500);
        }
        return map;
    }

    @Override
    public goodsVo getGood(String goodId) {
        Goods good = this.baseMapper.selectById(goodId);
        goodsVo vo = new goodsVo();
        BeanUtils.copyProperties(good,vo);
        Long end = dateUtils.getTimeStamp(good.getEnd_time(),1);
        Long up = dateUtils.getTimeStamp(good.get_updateTime(),1);
        vo.set_updateTime(up);
        vo.setEnd_time(end);
        List<String> url = imagesMapper.selectList(new QueryWrapper<Images>().eq("goods_id",good.getId()))
                .stream().map(images -> {

                    return images.getImagesUrl();
                }).collect(Collectors.toList());

        vo.setImages(url);
        return vo;
    }

    @Override
    public List<goodsVo> search(String key) {
        List<Goods> goods = mapper.search(key);
       return this.GoodsVoRes(goods);
    }

    @Override
    public List<goodsVo> getMyGoods(String publisherId) {
        List<Goods> goods = baseMapper.getMyGoods(publisherId);
        return this.GoodsVoRes(goods);
    }

    @Override
    public List<goodsVo> getDeal(String buyerOpenId) {
        List<Goods> goods = baseMapper.getDeal(buyerOpenId);
        return this.GoodsVoRes(goods);
    }
    //可重复利用代码
    private List<goodsVo> GoodsVoRes(List<Goods> goods){
        List<goodsVo> goodsVos = goods.stream()
                .map(v -> {
                    goodsVo vo = new goodsVo();
                    BeanUtils.copyProperties(v,vo);
                    Long end = dateUtils.getTimeStamp(v.getEnd_time(),1);
                    Long up = dateUtils.getTimeStamp(v.get_updateTime(),1);
                    vo.set_updateTime(up);
                    vo.setEnd_time(end);
                    List<String> url = imagesMapper.selectList(new QueryWrapper<Images>().eq("goods_id",v.getId()))
                            .stream().map(images -> {

                                return images.getImagesUrl();
                            }).collect(Collectors.toList());

                    vo.setImages(url);
                    return vo;
                }).collect(Collectors.toList());
        return goodsVos;
    }
}
